home *** CD-ROM | disk | FTP | other *** search
/ Aminet 28 / Aminet 28 (1998)(GTI - Schatztruhe)[!][Dec 1998].iso / Aminet / dev / c / qtools0.2-src.lha / src / libqdisplay / tbsp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-07-19  |  3.8 KB  |  140 lines

  1. #define    LIBQDISPLAY_CORE
  2. #include "../include/libqdisplay.h"
  3.  
  4. /*
  5.  * model: a combination of a brush and an entity.
  6.  * 
  7.  * One or more brushes are bound to an entity,
  8.  * which controls the behavior of the brushes.
  9.  * All brushes are contained within models.
  10.  * 
  11.  * The model numbers in the compiled BSP (*x) comes from the order in which
  12.  * the models are stored in the models structure. These numbers are originally
  13.  * derived from the order of the models in the source MAP file.
  14.  * 
  15.  * The worldspawn model is a bounding box that defines the extents of the
  16.  * whole world.
  17.  * 
  18.  * The models are defined by a bounding box of the max and min(x,y,z).
  19.  * Therefore they are always parallel to the horizontal planes.
  20.  * -- (qkspecs.)
  21.  */
  22.  
  23. /*
  24.  * 
  25.  */
  26. static bool camera_plane_test(register struct dplane_t *plane)
  27. {
  28. #if (PLANE_X == 0) && (PLANE_Y == 1) && (PLANE_Z == 2)
  29.   vec_t val;
  30.  
  31.   if (plane->type < PLANE_Z)
  32.     val = cameraLocation[plane->type];
  33.   else
  34.     val = DotProduct(plane->normal, cameraLocation);
  35.  
  36.   return val < plane->dist;
  37. #else
  38.   switch (plane->type) {
  39.     case PLANE_X:
  40.       return cameraLocation[0] < plane->dist;
  41.       break;
  42.     case PLANE_Y:
  43.       return cameraLocation[1] < plane->dist;
  44.       break;
  45.     case PLANE_Z:
  46.       return cameraLocation[2] < plane->dist;
  47.       break;
  48.     default:
  49.       return DotProduct(plane->normal, cameraLocation) < plane->dist;
  50.       break;
  51.   }
  52. #endif
  53. }
  54.  
  55. /*
  56.  * there could be three different values in children[]
  57.  *  -1          terminator, references to dleaf[0]      is set if childrens planenum == -1 and CONTENTS_SOLID
  58.  *  negative    what follows are leafs                  is set otherwise
  59.  *  positive    what follows are nodes                  is set if childrens planenum != -1
  60.  */
  61.  
  62. short int find_leaf(__memBase)
  63. {
  64.   short int n = (short int)bspMem->shared.quake1.dmodels[model].headnode[0];
  65.   struct dnode_t *node;
  66.  
  67.   do {
  68.     node = &bspMem->shared.quake1.dnodes[n];
  69.   } while ((n = node->children[camera_plane_test(&bspMem->shared.quake1.dplanes[node->planenum])]) >= 0);
  70.  
  71.   return ~n;
  72. }
  73.  
  74. void bsp_render_node(__memBase, short int node)
  75. {
  76.   if (node >= 0) {
  77.     if (is_marked_node(node)) {
  78.       if (camera_plane_test(&bspMem->shared.quake1.dplanes[bspMem->shared.quake1.dnodes[node].planenum])) {
  79.     bsp_render_node(bspMem, bspMem->shared.quake1.dnodes[node].children[0]);
  80.     render_node_faces(bspMem, node, 1);
  81.     bsp_render_node(bspMem, bspMem->shared.quake1.dnodes[node].children[1]);
  82.       }
  83.       else {
  84.     bsp_render_node(bspMem, bspMem->shared.quake1.dnodes[node].children[1]);
  85.     render_node_faces(bspMem, node, 0);
  86.     bsp_render_node(bspMem, bspMem->shared.quake1.dnodes[node].children[0]);
  87.       }
  88.       unmark_node(node);
  89.     }
  90.   }
  91. }
  92.  
  93. void bsp_explore_node(__memBase, short int node)
  94. {
  95.   if (node >= 0) {
  96.     if (is_marked_node(node)) {
  97.       if (!node_in_frustrum(&bspMem->shared.quake1.dnodes[node])) {
  98.     unmark_node(node);
  99.       }
  100.       else {
  101.     bsp_explore_node(bspMem, bspMem->shared.quake1.dnodes[node].children[0]);
  102.     bsp_explore_node(bspMem, bspMem->shared.quake1.dnodes[node].children[1]);
  103.       }
  104.     }
  105.   }
  106.   else {
  107.     /*
  108.      * node is 0 or a valid leaf-node
  109.      * if 0 everything is invisible (leaf 0 is CONTENTS_SOLID)
  110.      * else it is something other
  111.      */
  112.     node = ~node;
  113.  
  114.     if (is_marked_leaf(node))
  115.       if (leaf_in_frustrum(&bspMem->shared.quake1.dleafs[node]))
  116.     mark_leaf_faces(bspMem, node);
  117.   }
  118. }
  119.  
  120. /*
  121.  * recursively determine which nodes need exploring (so we
  122.  * don't look for polygons on _every_ node in the level)
  123.  *
  124.  * this need only be called once at the beginning
  125.  */
  126. int bsp_find_visible_nodes(__memBase, short int node)
  127. {
  128.   if (node >= 0) {
  129.     if (bsp_find_visible_nodes(bspMem, bspMem->shared.quake1.dnodes[node].children[0]) |
  130.     bsp_find_visible_nodes(bspMem, bspMem->shared.quake1.dnodes[node].children[1]))
  131.       return mark_node(node);
  132.     else
  133.       return 0;
  134.   }
  135.   else {
  136.     node = ~node;
  137.     return is_marked_leaf(node);
  138.   }
  139. }
  140.